<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="帖子标题" prop="postTitle">
        <el-input
          v-model="queryParams.postTitle"
          placeholder="请输入帖子标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['community:post:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['community:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['community:post:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['community:post:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="帖子ID" align="center" prop="postId" />
      <el-table-column label="发帖用户ID" align="center" prop="postUserId" />
      <el-table-column label="帖子所属项目ID" align="center" prop="postAllId" />
      <el-table-column label="帖子标题" align="center" prop="postTitle" />
      <el-table-column label="帖子内容" align="center" prop="postContent" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['community:post:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['community:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社区帖子对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="帖子标题" prop="postTitle">
          <el-input v-model="form.postTitle" placeholder="请输入帖子标题" />
        </el-form-item>
        <el-form-item label="帖子内容" >
          <el-input v-model="form.postContent" :min-height="100"/>
        </el-form-item>
        <el-form-item label="帖子所属项目ID"  v-dialog-drag-height="100" label-width="20">
          <el-input v-model="form.postAllId" placeholder="请输入帖子所属项目ID"/>
        </el-form-item>
        <el-form-item label="发帖用户ID">
          <el-input v-model="form.postUserId" placeholder="请输入发帖用户ID"/>
        </el-form-item>
        <el-divider content-position="center">社区评论信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCommunityComments">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCommunityComments">删除</el-button>
          </el-col>
        </el-row>
        <el-table :gutter="10" :data="communityCommentsList" :row-class-name="rowCommunityCommentsIndex" @selection-change="handleCommunityCommentsSelectionChange" ref="communityComments">
          <el-table-column  :span="4" type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="评论用户ID" prop="commentsUserId" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.commentsUserId" placeholder="请输入评论用户ID" />
            </template>
          </el-table-column>

          <el-table-column label="评论内容" prop="commentsContent">
            <template slot-scope="scope">
              <el-input v-model="scope.row.commentsContent" placeholder="请输入评论内容" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="commentsCreateTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.commentsCreateTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="点赞数" prop="commentsUp" width="80">
            <template slot-scope="scope">
              <el-input v-model="scope.row.commentsUp" placeholder="请输入点赞数" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/community/post";

export default {
  name: "Post",
  data() {
    return {
      //添加评论的实时时间
      times:'',//格式化之后的当前时间
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCommunityComments: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社区帖子表格数据
      postList: [],
      // 社区评论表格数据
      communityCommentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postUserId: null,
        postAllId: null,
        postTitle: null,
        postContent: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postUserId: [
          { required: true, message: "发帖用户ID不能为空", trigger: "change" }
        ],
        postAllId: [
          { required: true, message: "帖子所属项目ID不能为空", trigger: "change" }
        ],
        postContent: [
          { required: true, message: "帖子内容不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getTimes(),
    this.getList();
  },
  // 在Vue实例销毁前，清除我们的定时器
  beforeDestroy() {
    if (this.times) {
      clearInterval(this.getTimesInterval);
    }
  },
  methods: {
    //实时时间
    getTimes(){
      setInterval(this.getTimesInterval, 1000);
    },
    getTimesInterval:function() {
      let _this = this;
      let year = new Date().getFullYear(); //获取当前时间的年份
      let month = new Date().getMonth() + 1; //获取当前时间的月份
      let day = new Date().getDate(); //获取当前时间的天数
      let hours = new Date().getHours(); //获取当前时间的小时
      let minutes = new Date().getMinutes(); //获取当前时间的分数
      let seconds = new Date().getSeconds(); //获取当前时间的秒数
      //当小于 10 的是时候，在前面加 0
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minutes < 10) {
        minutes = "0" + minutes;
      }
      if (seconds < 10) {
        seconds = "0" + seconds;
      }
      //拼接格式化当前时间
      _this.times = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
      this.times =_this.times

    },
    /** 查询社区帖子列表 */
    getList() {
      this.loading = true;

      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: null,
        postUserId: null,
        postAllId: null,
        postTitle: null,
        postContent: null,
        createTime: null
      };
      this.communityCommentsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社区帖子";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.form = response.data;
        this.communityCommentsList = response.data.communityCommentsList;
        this.open = true;
        this.title = "修改社区帖子";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.communityCommentsList = this.communityCommentsList;
          if (this.form.postId != null) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal.confirm('是否确认删除社区帖子编号为"' + postIds + '"的数据项？').then(function() {
        return delPost(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 社区评论序号 */
    rowCommunityCommentsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 社区评论添加按钮操作 */
    handleAddCommunityComments() {
      let obj = {};
      obj.commentsContent = "";
      obj.commentsCreateTime = "";
      obj.commentsUserId = "";
      obj.commentsUp = "";
      this.communityCommentsList.push(obj);
    },
    /** 社区评论删除按钮操作 */
    handleDeleteCommunityComments() {
      if (this.checkedCommunityComments.length == 0) {
        this.$modal.msgError("请先选择要删除的社区评论数据");
      } else {
        const communityCommentsList = this.communityCommentsList;
        const checkedCommunityComments = this.checkedCommunityComments;
        this.communityCommentsList = communityCommentsList.filter(function(item) {
          return checkedCommunityComments.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCommunityCommentsSelectionChange(selection) {
      this.checkedCommunityComments = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('community/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
